package greatwall.myreader;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.iximo.domain.Book;
import com.iximo.openAPI.impl.IximoOpenAPIGetImpl;
import com.iximo.util.OpenAPIClient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ShowBookListByCategory extends Activity
{
	private IximoOpenAPIGetImpl Get = new IximoOpenAPIGetImpl();
	private TextView result;
	private OpenAPIClient XiMoClient = OpenAPIClientFactory.GetInstance();
	ArrayList<HashMap<String,Object > > listItem;
	private ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.getbooksbycategory);
		result = (TextView)super.findViewById(R.id.Display);
		ReceiveCategoryObj();
	}
	public void ReceiveCategoryObj()
	{
		try 
		{
			Intent intent = getIntent();
			String getstring = intent.getStringExtra("getString");
//			InputStream inputStream = new ByteArrayInputStream(getstring.getBytes());
			int cateId = Integer.parseInt(getstring);
		    String getBookStringByCategory = XiMoClient.get_book_category_sort_list(cateId, "comment", 1, 15, "");
		    String tempBook = getBookStringByCategory.replace("allPage", "pages");		    
		    InputStream inputStream = new ByteArrayInputStream(tempBook.getBytes());		    
		    List<Book> getBookListByCategory = Get.getBookByNumData(inputStream);
		    list = (ListView) findViewById(R.id.LVcategorybook);
		    listItem = new ArrayList<HashMap<String, Object>>();
			             
		    for(int i=0;i<getBookListByCategory.size();i++)   
	        {   
		    	Log.i("size",getBookListByCategory.size() + "");
		    	 HashMap<String, Object> map = new HashMap<String, Object>();     
		         map.put("ItemImage", R.drawable.book_bg_shadow);//图像资源的ID
//		         map.put("ItemImage",getBookListByCategory.get(i).getLimageURL());
		         map.put("BookName","书名：" + getBookListByCategory.get(i).getTitle());   
		         map.put("Writer","作者：" +  getBookListByCategory.get(i).getAuthor());
		         map.put("Visitor", "人气：" + getBookListByCategory.get(i).getHitCount());
		         map.put("Size", "字数：" + getBookListByCategory.get(i).getWords());
		         map.put("Classification", "所属分类： " + getBookListByCategory.get(i).getCategoryName()); 
	             map.put("BookID", getBookListByCategory.get(i).getBookID());
	             listItem.add(map);
	             }  
		    Log.i("i","w");	  
		  //生成适配器的Item和动态数组对应的元素   
	        SimpleAdapter listItemAdapter = new SimpleAdapter(ShowBookListByCategory.this,listItem,//数据源    
	            R.layout.searchbookuser,//ListItem的XML实现   
	            //动态数组与ImageItem对应的子项           
	            new String[] {"ItemImage","BookName", "Writer","Visitor","Size","Classification"},    
	            //ImageItem的XML文件中的一张图片和三个TextView ID   
	            new int[] {R.id.ItemImage,R.id.BookName,R.id.Writer,R.id.Visitor,R.id.Size,R.id.Classification}   
	        );   
	        list.setAdapter(listItemAdapter); 
	        Log.i("aa", "bb");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Toast.makeText(ShowBookListByCategory.this, "bbb", 0);
		}
		
	}
}
